package com.ddlaw.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ddlaw.entity.TCity;
import com.ddlaw.entity.TLawQuestionCustom;
import com.ddlaw.entity.TLawReply;
import com.ddlaw.entity.TLawReplyCustom;
import com.ddlaw.entity.TLawyer;
import com.ddlaw.entity.TLawyerDetailCustom;
import com.ddlaw.entity.TLawyerServiceType;
import com.ddlaw.entity.TLawyersApply;
import com.ddlaw.entity.TStudentDetaiCustom;
import com.ddlaw.entity.TUsers;
import com.ddlaw.service.LawQuestionService;
import com.ddlaw.service.LawyerApplyService;
import com.ddlaw.service.LawyerDetailService;
import com.ddlaw.service.LawyerTypeService;
import com.ddlaw.service.ProvinceService;
import com.ddlaw.service.UserService;
import com.ddlaw.util.XmlReadUtil;
@Controller
@RequestMapping(value="lawyer")
public class LawyerController extends BaseController{
		
		@Resource
		ProvinceService provinceService;
		@Resource
		LawyerTypeService lawyerTypeService;
		@Resource
		LawyerApplyService applyService;
		@Resource
		LawyerDetailService detailService;
		@Resource
		LawQuestionService questionService;
		@Resource
		UserService userService;
		
		/**
		 * 律师申请页面
		 * @param model
		 * @return
		 */
		@RequestMapping(value="register",method=RequestMethod.GET)
		public String register(Model model){
			List<TLawyerServiceType> lawyerTypes = lawyerTypeService.getLawyerServiveType(0);
			model.addAttribute("lawyerTypes",lawyerTypes );
			return "lawyer/apply";
		}
		
		/**
		 * 获取律师申请省份城市信息
		 * @param provinceId		省份ID
		 * @return		对应城市
		 */
		@RequestMapping(value="province",method=RequestMethod.GET)
		@ResponseBody
		public Map<String,Object> province( int provinceId){
			Map<String,Object> result = new HashMap<String, Object>();
			List<TCity> citys = provinceService.getCityByProId(provinceId);
			result.put("citys", citys);
			return result;
		}
		
		/**
		 * 律师申请信息录入
		 * @param lawyersApply		律师详细信息
		 * @return		录入申请表
		 */
		@RequestMapping(value="register",method=RequestMethod.POST)
		public String applyLawyer(TLawyersApply lawyersApply){
			TUsers  users = (TUsers)getHttpSession().getAttribute("user");
			lawyersApply.setUserid(users.getUserid());
			applyService.addLawyersApply(lawyersApply);
			return "user/suc";
		}
		
		/***
		 * 导航法律人获取信息
		 * @param model
		 * @return
		 */
		@RequestMapping(value="list",method=RequestMethod.GET)
		public String lawyers(Model model,
				@RequestParam(value="typeId",defaultValue="0") Integer typeId,
				@RequestParam(value="yearId",defaultValue="0") Integer yearId,
				@RequestParam(value="province",defaultValue="0") Integer province,
				@RequestParam(value="city",defaultValue="0") Integer city){
			Map<String,Object> params = new HashMap<String,Object>();
			params.put("typeId",typeId);
			params.put("yearId",yearId);
			params.put("province",province);
			params.put("city",city);
			Map<String,String> specials = XmlReadUtil.read("lawServiceType.xml", LawyerController.class);
			List<TLawyerServiceType>  serviceTypes = lawyerTypeService.getLawyerServiveType(0);
			List<TLawyerDetailCustom>  lawyersDetails = detailService.getAllLawyers(params);
			for(TLawyerDetailCustom lawyer:lawyersDetails){
				lawyer.setSpecialNameA(specials.get(lawyer.getSpeciala()+""));
				lawyer.setSpecialNameB(specials.get(lawyer.getSpecialb()+""));
				lawyer.setSpecialNameC(specials.get(lawyer.getSpecialc()+""));
			}
			List<TStudentDetaiCustom>  studentsDetails = detailService.getAllOtherLawyers(params);
			for(TStudentDetaiCustom sttudent:studentsDetails){
				sttudent.setSpecialNameA(specials.get(sttudent.getSpeciala()+""));
			}
			List<TCity> citys = provinceService.getCityByProId(province);
			List<TLawyerDetailCustom>  recomLawyer = detailService.getLawyerByState(2);
			model.addAttribute("serviceTypes", serviceTypes);
			model.addAttribute("lawyersDetails", lawyersDetails);
			model.addAttribute("studentsDetails", studentsDetails);
			model.addAttribute("recomLawyer", recomLawyer);
			model.addAttribute("typeId", typeId);
			model.addAttribute("yearId", yearId);
			model.addAttribute("province", province);
			model.addAttribute("city", city);
			model.addAttribute("citys", citys);
			return "lawyer/lawyer";
		}
		/**
		 * 律师详情页
		 * @param model
		 * @param lawyerId	律师ID
		 * @return		律师详细信息
		 */
		@RequestMapping(value="detail/{lawyerId}",method=RequestMethod.GET)
		public String lawyerDetail(Model model,@PathVariable(value="lawyerId") int lawyerId){
			Map<String,Object> params = new HashMap<String,Object>();
			params.put("lawyerId",lawyerId );
			List<TLawyerDetailCustom>  lawyersDetails = detailService.getAllLawyers(params);
			Map<String,String> specials = XmlReadUtil.read("lawServiceType.xml", LawyerController.class);
			for(TLawyerDetailCustom lawyer:lawyersDetails){
				lawyer.setSpecialNameA(specials.get(lawyer.getSpeciala()+""));
				lawyer.setSpecialNameB(specials.get(lawyer.getSpecialb()+""));
				lawyer.setSpecialNameC(specials.get(lawyer.getSpecialc()+""));
			}
			List<TLawReplyCustom> lawQuession = questionService.getQuesById(lawyerId);
			model.addAttribute("lawyersDetails", lawyersDetails.get(0));
			model.addAttribute("lawQuession", lawQuession);
			return "lawyer/detail";
		}
		
		
		/**
		 * 其他律师详情页
		 * @param model
		 * @param lawyerId	律师ID
		 * @return		律师详细信息
		 */
		@RequestMapping(value="other/detail/{lawyerId}",method=RequestMethod.GET)
		public String lawyerOtherDetail(Model model,@PathVariable(value="lawyerId") int lawyerId){
			Map<String,Object> params = new HashMap<String,Object>();
			params.put("lawyerId",lawyerId );
			List<TStudentDetaiCustom>  lawyersDetails = detailService.getAllOtherLawyers(params);
			Map<String,String> specials = XmlReadUtil.read("lawServiceType.xml", LawyerController.class);
			for(TStudentDetaiCustom lawyer:lawyersDetails){
				lawyer.setSpecialNameA(specials.get(lawyer.getSpeciala()+""));
			}
			List<TLawReplyCustom> lawQuession = questionService.getQuesById(lawyerId);
			model.addAttribute("lawyersDetails", lawyersDetails.get(0));
			model.addAttribute("lawQuession", lawQuession);
			return "lawyer/other-detail";
		}
		
		/**
		 * 律师个人中心
		 * @param model
		 * @return
		 */
		@RequestMapping(value="/person",method=RequestMethod.GET)
		public String person(Model model){
			TUsers users = (TUsers) getHttpSession().getAttribute("user");
			TLawyer lawyer = detailService.getLawyerByUserId(users.getUserid());
			if(lawyer != null){
				List<TLawQuestionCustom> questions = questionService.getNoAnsQues(lawyer.getLawyerId(), 0);
				model.addAttribute("questions",questions);
			}
			return "lawyer/personal";
		}
		
		@RequestMapping(value="/answer",method=RequestMethod.POST)
		@ResponseBody
		public Map<String,Object> ansQues(@RequestParam(value="replycontent")  String replycontent,
																				 @RequestParam(value="quesid") int quesid){
			Map<String,Object> result = new HashMap<String,Object>();
			TUsers users = (TUsers) getHttpSession().getAttribute("user");
			TLawyer lawyer = null;
			if(users != null){
				 lawyer = detailService.getLawyerByUserId(users.getUserid());
			}
			if(lawyer != null){
				TLawReply lawReply = new TLawReply();
				lawReply.setReplycontent(replycontent);
				lawReply.setQuesid(quesid);
				lawReply.setAnswererid(lawyer.getLawyerId());
				questionService.addAnsToUser(lawReply);
				questionService.updateQuesState(1, quesid);
				result.put("SUCCESS_CODE",200);
				result.put("MSG","回复成功！");
			}else{
				result.put("ERROR_CODE",100);
				result.put("MSG","系统繁忙，请稍后再试！");
			}
			return result;
		}
		@RequestMapping(value="/answer",method=RequestMethod.GET)
		@ResponseBody
		public Map<String,Object> ansQuesByState(){
			Map<String,Object> result = new HashMap<String,Object>();
			Map<String,Object> params = new HashMap<String,Object>();
			TUsers users = (TUsers) getHttpSession().getAttribute("user");
			TLawyer lawyer = detailService.getLawyerByUserId(users.getUserid());
			if(lawyer != null){
				params.put("lawyerId", lawyer.getLawyerId());
				params.put("state", 1);
				List<TLawReplyCustom> questions = questionService.getLawyerAns(params);
				result.put("answered", questions);
				result.put("SUCCESS_CODE",200);
			}else{
				result.put("ERROR_CODE",100);
				result.put("MSG","系统繁忙，请稍后再试！");
			}
				return result;
		}
}
